package flc.ast.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;

import com.blankj.utilcode.util.FileUtils;
import com.blankj.utilcode.util.ImageUtils;
import com.blankj.utilcode.util.PathUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.adapter.DraftAdapter;
import flc.ast.databinding.ActivityDraftsBinding;
import flc.ast.dialog.EditDialog;
import flc.ast.utils.Content;
import io.reactivex.rxjava3.core.ObservableEmitter;
import stark.common.basic.utils.FileP2pUtil;
import stark.common.basic.utils.FileUtil;
import stark.common.basic.utils.IntentUtil;
import stark.common.basic.utils.RxUtil;

public class DraftsActivity extends BaseAc<ActivityDraftsBinding> {

    private DraftAdapter draftAdapter;
    private List<String> listShow = new ArrayList<>();

    @Override
    protected int onCreate() {
        return R.layout.activity_drafts;
    }

    @Override
    protected void initView() {
        mDataBinding.ivDraftBack.setOnClickListener(v -> onBackPressed());

        mDataBinding.ivClear.setOnClickListener(this);

        mDataBinding.rvDraftList.setLayoutManager(new GridLayoutManager(mContext, 2));
        draftAdapter = new DraftAdapter();
        mDataBinding.rvDraftList.setAdapter(draftAdapter);
        draftAdapter.addChildClickViewIds(R.id.ivDraftItemEdit);
        draftAdapter.setOnItemChildClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.ivClear://清除
                clear();
                break;
        }
    }

    @Override
    public void onItemChildClick(@NonNull BaseQuickAdapter adapter, @NonNull View view, int position) {
        switch (view.getId()) {
            case R.id.ivDraftItemEdit://编辑
                showEditDialog(position);
                break;
        }
    }

    @Override
    protected void initData() {
        getData();
    }

    private void getData() {
        listShow.clear();
        //获取文件夹下文件
        String path = PathUtils.getFilesPathExternalFirst() + Content.myDrafts;
        List<File> files = FileUtils.listFilesInDir(path);
        if (files.size() > 0) {
            for (File file : files) {
                listShow.add(file.getPath());
            }
        }
        if (listShow.size() > 0) {
            draftAdapter.setList(listShow);
            mDataBinding.rvDraftList.setVisibility(View.VISIBLE);
            mDataBinding.tvDraftNoData.setVisibility(View.GONE);
        } else {
            mDataBinding.rvDraftList.setVisibility(View.GONE);
            mDataBinding.tvDraftNoData.setVisibility(View.VISIBLE);
        }
    }

    private void showEditDialog(int position) {
        EditDialog dialog = new EditDialog(mContext);

        dialog.setListener(new EditDialog.Ilistener() {
            @Override
            public void clickShare() {
                IntentUtil.shareVideo(mContext, listShow.get(position));
            }

            @Override
            public void clickSave() {
                saveToAlbum(position);
            }

            @Override
            public void clickDelete() {
                delete(position);
            }
        });

        dialog.show();
    }

    //保存图片
    private void saveToAlbum(int position) {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                RxUtil.create(new RxUtil.Callback<String>() {

                    @Override
                    public void doBackground(ObservableEmitter<String> emitter) {
                        FileP2pUtil.copyPrivateImgToPublic(mContext, listShow.get(position));
                        emitter.onNext("");
                    }

                    @Override
                    public void accept(String path) {
                        ToastUtils.showShort(getString(R.string.save_success));
                    }
                });
            }
        }, 1000);
    }

    private void delete(int position) {
        showDialog(getString(R.string.delete_ing));
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                RxUtil.create(new RxUtil.Callback<String>() {

                    @Override
                    public void doBackground(ObservableEmitter<String> emitter) {
                        //从此文件夹删除
                        FileUtils.delete(listShow.get(position));
                        emitter.onNext("");
                    }

                    @Override
                    public void accept(String path) {
                        getData();
                        dismissDialog();
                        ToastUtils.showShort(getString(R.string.delete_suc));
                    }
                });
            }
        }, 1000);
    }

    private void clear() {
        showDialog(getString(R.string.delete_ing));
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                RxUtil.create(new RxUtil.Callback<String>() {

                    @Override
                    public void doBackground(ObservableEmitter<String> emitter) {
                        //从此文件夹删除
                        for (String s : listShow) {
                            FileUtils.delete(s);
                        }
                        emitter.onNext("");
                    }

                    @Override
                    public void accept(String path) {
                        getData();
                        dismissDialog();
                        ToastUtils.showShort(getString(R.string.clear_suc));
                    }
                });
            }
        }, 1000);
    }

}